package timing.ukulele.third.controller.weixin;

import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.third.persistent.weixin.WxMenuRule;
import timing.ukulele.third.service.weixin.WxMenuService;
import timing.ukulele.third.model.weixin.Menu;

/**
 * 微信自定义菜单管理
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/wxMenu")
public class WxMenuController {

	private final WxMenuService wxMenuService;

	/**
	 * 通过appId和menuRuleId查询菜单
	 *
	 * @param appId
	 * @param menuRuleId
	 * @return R
	 */
	@Operation(summary = "通过appId和menuRuleId查询菜单")
	@GetMapping("/getWxMenu")
	public ResponseData<Menu> getWxMenu(String appId, String menuRuleId) {
		return ResponseData.success(wxMenuService.getWxMenu(appId, menuRuleId));
	}

    /**
     * /wxMenu/release
     * @param menu
     * @return
     */
	@Operation(summary = "保存并发布菜单")
	@PostMapping("/release")
	public ResponseData<WxMenuRule> saveAndRelease(@RequestBody Menu menu) {
		try {
			return ResponseData.success(wxMenuService.saveAndRelease(menu));
		} catch (WxErrorException e) {
            return ResponseData.error("发布自定义菜单失败appID:" + menu.getAppId() + ":" + e.getMessage());
		}
	}

	/**
	 * 删除菜单
	 *
	 * @param ruleId
	 * @return R
	 */
	@Operation(summary = "删除菜单")
	@DeleteMapping("deleteWxMenu")
	public ResponseData<Boolean> deleteWxMenu(@RequestParam("ruleId") String ruleId) {
		try {
			return ResponseData.success(wxMenuService.deleteWxMenu(ruleId));
		} catch (WxErrorException e) {
            return ResponseData.error("删除微信菜单失败ruleId:" + ruleId + ":" + e.getMessage());
		}
	}

}
